﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Infrastructure.Dapper;
using WoChu.Infrastructure.Data;
using WoChu.WMS.Entity;

namespace WoChu.WMS.Repository
{
    /// <summary>
    /// 订单主表
    /// </summary>
    public class SaleAPPRepository : RepositoryBase<SaleAPP, string>
    {
        public SaleAPPRepository() : base(ConnectionsDBKey.ERP_Ora) { }
        public List<SaleAPP> GetCheckList(SaleAPP entity)
        {
            StringBuilder sql = new StringBuilder();
            StringBuilder whereSql = new StringBuilder();
            sql.Append(@"
            Select sal.SHEETID,SALEORDERFLAG,RATIONDATE,RATIONTIME,TICKETNO,SITEID,DELIVERYSTATUS,POINTNAME,APPORDERNO
            ,li.lookup DeliveryTimePeriod,sai.SEQNO,sai.SHEETID,sai.REMARK,sai.QTY
            From SaleAPP sal 
            inner join  lookupitem li on sal.RATIONTIME=li.fieldvalue and li.lookupid='RATIONTIME' 
            inner join SaleAPPItem1 sai  on sal.SHEETID=sai.SHEETID
            Where 1=1
            ");
            
            if (entity.RATIONDATE!=null)
                whereSql.Append(" And RATIONDATE=:RATIONDATE");
            if (!string.IsNullOrEmpty(entity.SHEETID))
                whereSql.Append(" And sal.SHEETID=:SHEETID");
            sql.Append(whereSql);
            var list = new Dictionary<string, SaleAPP>();
            QueryMulti<SaleAPP, SaleAPPItem>(sql.ToString(), entity,
                (sale, detail) =>
                {
                    SaleAPP s;
                    if (!list.TryGetValue(sale.SHEETID, out s))
                    {
                        s = sale;
                        list.Add(sale.SHEETID, sale);
                    }
                    s.SaleAPPItemList.Add(detail);
                    return sale;
                },
                "SHEETID");
            return list.Values.ToList();
        }
    }
}
